﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UIElements.ViewModel;

namespace UIElements
{
    /// <summary>
    /// Interaction logic for AddNewLocationControl.xaml
    /// </summary>
    public partial class AddNewLocationControl : UserControl
    {
        #region Private Fields

        private Window hostWindow;
        private MainControlViewModel viewModel;

        #endregion

        public AddNewLocationControl(MainControlViewModel viewModel)
        {
            InitializeComponent();

            // initialize host window
            hostWindow = new Window();
            hostWindow.SizeToContent = SizeToContent.WidthAndHeight;
            hostWindow.ResizeMode = ResizeMode.NoResize;
            hostWindow.WindowStyle = WindowStyle.None;
            hostWindow.BorderThickness = new Thickness(1, 1, 1, 1);

            // set view model
            this.viewModel = viewModel;
        }

        public void Popup()
        {
            hostWindow.Content = this;
            hostWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            hostWindow.ShowDialog();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            hostWindow.Close();
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            // check if the location name is empty
            if (string.IsNullOrEmpty(tbxLocationName.Text.Trim()))
            {
                // display error
                tblxError.Text = "Please enter location name";
                return;
            }

            // check if the location path is empty
            if (string.IsNullOrEmpty(tbxLocationPath.Text.Trim()))
            {
                // display error
                tblxError.Text = "Please enter location path";
                return;
            }

            // create new location with specified name and path
            viewModel.AddNewLocation(tbxLocationName.Text.Trim(), tbxLocationPath.Text.Trim());

            // close the window
            hostWindow.Close();
        }

        private void btnSelectPath_Click(object sender, RoutedEventArgs e)
        {
            string locationPath = string.Empty;

            var folderPickerDialog = new FolderPickerLib.FolderPickerDialog();
            if (folderPickerDialog.ShowDialog() == true)
            {
                locationPath = folderPickerDialog.SelectedPath;
            }

            this.tbxLocationPath.Text = locationPath;
        }
    }
}
